package com.weetop.aklyscan.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.appConfig.MyApplication;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.base.mvp.BasePresenter;
import com.weetop.aklyscan.base.mvp.BaseView;
import com.weetop.aklyscan.bean.PostMessage;
import com.weetop.aklyscan.utils.BaseUtils;
import com.weetop.aklyscan.utils.EventBusUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * File descripition: activity基类
 * <p>
 *
 * @author lp
 * @date 2018/5/16
 */
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView {
    protected final String TAG = this.getClass().getSimpleName();
    public Context mContext;
    protected P mPresenter;
    public MyApplication myApplication;
    protected Activity mActivity;
    protected boolean isClear = false;
    protected int pageNum = 1;
    protected int pageSize = 15;

    protected abstract P createPresenter();

    //错误提示框  警告框  成功提示框 加载进度框 （只是提供个案例 可自定义）
    protected QMUITipDialog tipDialog;
//
//    @Override
//    protected boolean translucentFull() {
//        return true;
//    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        myApplication = (MyApplication) getApplication();
        mActivity = BaseActivity.this;
        mPresenter = createPresenter();
        if (isRegisterEventBus()) {
            EventBusUtil.register(this);
        }
        setTitleBarStytle(mActivity);
    }

    protected void setToolBar(Toolbar toolbar) {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    protected void setToolBar(String ceterText, String rightText) {
        Toolbar toolbar = findViewById(R.id.toolbar);
        TextView tv_tl_title = findViewById(R.id.tv_tl_title);
        TextView tv_tl_right = findViewById(R.id.tv_tl_right);

        tv_tl_title.setText(ceterText);
        tv_tl_right.setText(rightText);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    protected void setToolBar(String ceterText, String rightText, View.OnClickListener clickListener) {
        Toolbar toolbar = findViewById(R.id.toolbar);
        TextView tv_tl_title = findViewById(R.id.tv_tl_title);
        TextView tv_tl_right = findViewById(R.id.tv_tl_right);

        tv_tl_title.setText(ceterText);
        tv_tl_right.setText(rightText);

        tv_tl_right.setOnClickListener(clickListener);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    protected void setTitleBarStytle(Activity activity) {
        BaseUtils.setTitleBarColor(activity);
        QMUIStatusBarHelper.setStatusBarLightMode(activity);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }


    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    /**
     * 接口加载
     *
     * @return
     */
    protected boolean isShowNetLoading() {
        return true;
    }

    protected void showDialog(String str) {
        if (str.equals("")) {
            tipDialog = new QMUITipDialog.Builder(mActivity)
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                    .create();
        } else {
            tipDialog = new QMUITipDialog.Builder(mActivity)
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                    .setTipWord(str)
                    .create();
        }

        tipDialog.show();
    }


    protected void dissmissDialog() {
        if (tipDialog != null)
            tipDialog.dismiss();
    }

    @Override
    public void showError(String msg) {
        Log.e("weetop", "error: " + msg);
    }

    @Override
    public void onDataError() {

    }

    @Override
    public void onError() {

    }

    @Override
    public void onUpFail(String mag) {
    }

    @Override
    public void showLoading() {
        if (isShowNetLoading())
            showDialog("请稍后");
    }

    @Override
    public void hideLoading() {
        if (isShowNetLoading())
            dissmissDialog();
    }

    @Override
    public void onErrorCode(BaseModel model) {
        if (model != null) {
            if (model.getCode() == 888) {
                final AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(BaseActivity.this);
                normalDialog.setTitle("提示");
                normalDialog.setMessage("账号已在其他地方登陆");
                normalDialog.setPositiveButton("重新登陆", (dialog, which) -> {
                    dialog.dismiss();
                });
                normalDialog.setOnDismissListener(dialog -> {
//                    MyApplication.setUserToken("login");
//                    Intent intent = new Intent(mActivity, LoginActivity.class);
//                    startActivity(intent);
//                    ActivityUtils.finishAllActivitiesExceptNewest();
//                    mActivity.finish();
                });
                normalDialog.show();
            } else {
                // ToastUtils.showShort(model.getMsg());
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (isRegisterEventBus()) {
            EventBusUtil.unregister(this);
        }
    }

    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    protected void receiveEvent(PostMessage event) {

    }

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    protected void receiveStickyEvent(PostMessage event) {

    }


    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventBusCome(PostMessage event) {
        if (event != null) {
            receiveEvent(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onStickyEventBusCome(PostMessage event) {
        if (event != null) {
            receiveStickyEvent(event);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // 进行数据处理

        super.onConfigurationChanged(newConfig);

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // 保存数据
    }

}
